# _*_ coding: utf-8 _*_
"""
Time:     2024/8/22
Author:   2051261
File:     factory.py
"""
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.base import BaseScheduler

_scheduler_instance: BaseScheduler = None  # 全局变量，用于缓存调度器实例


def get_config():
    """默认配置"""
    return {
        'apscheduler.jobstores.default': {
            'type': 'memory',
        },
        'apscheduler.executors.default': {
            'class': 'apscheduler.executors.pool:ThreadPoolExecutor',
            'max_workers': '20',
        },
        # 'apscheduler.job_defaults.max_instances': '1',  # default
        'apscheduler.timezone': 'Asia/Shanghai',
    }


# def new_scheduler():
#     from apscheduler.schedulers.background import BackgroundScheduler
#
#     return BackgroundScheduler(get_config())
def new_scheduler():
    """创建或获取缓存的调度器实例"""
    global _scheduler_instance
    if _scheduler_instance is None or not _scheduler_instance.running:
        _scheduler_instance = BackgroundScheduler(get_config())
        _scheduler_instance.start()  # 启动调度器
    return _scheduler_instance
